package leetcode.slidewindow;

import java.util.HashSet;
import java.util.Set;


/**
 * 3. 无重复字符的最长子串
 * <p>
 * 最长的不重复连续子串
 */
public class LengthOfLongestSubstring {

    public static void main(String[] args) {
        String s = "abcabcbb";
        new LengthOfLongestSubstring().maxSubLength(s);
    }

    public int maxSubLength(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        Set<Character> set = new HashSet<>();
        char[] arr = s.toCharArray();
        int maxLength = 0;
        int right = 0;
        for (int i = 0; i < arr.length; i++) {

            if (i > 0) {
                set.remove(arr[i - 1]);
            }

            while (right < arr.length) {
                if (!set.contains(arr[right])) {
                    set.add(arr[right]);
                    maxLength = Math.max(maxLength, set.size());
                    right++;
                } else {
                    break;
                }
            }
        }

        return maxLength;
    }
}










